﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmos.Hardware.BlockDevice;

namespace SplitOS
{
    public static class Filesystem
    {
        private static AtaPio ATA = null;
        private static Partition OSPartition = null;
        private static Cosmos.System.Filesystem.FAT.FatFileSystem FATFS = null;
        private static List<Cosmos.System.Filesystem.Listing.Base> FATFileList;
        public static byte Initialize()
        {
            try
            {
                    if (BlockDevice.Devices[0] is AtaPio)
                    {
                        Console.WriteLine("ATA Device found");
                        ATA = (AtaPio)BlockDevice.Devices[0];
                    }
                    Console.WriteLine("Partitioning...");
                    if (BlockDevice.Devices[0] is Partition)
                    {
                        Console.WriteLine("Partition found");
                        //OSPartition = (Partition)BlockDevice.Devices[0];
                    }
            }
            catch
            {
                // ATA Error
                return 1;
            }
            try
            {
                //Make a filesystem attached to the partition
                FATFS = new Cosmos.System.Filesystem.FAT.FatFileSystem(OSPartition);
                //Map the filesystem to the B drive
                Cosmos.System.Filesystem.FileSystem.AddMapping("B", FATFS);
                FATFileList = FATFS.GetRoot();
            }
            catch
            {
                // Partition Error
                return 2;
            }
            return 0;
        }
    }
}
